﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FifteenPuzzleGUI
{
    class Board
    {
        public Tile[,] board = new Tile[4, 4];

        public Board()
        {
            int i = 1;
            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 4; col++ )
                {
                    board[row, col] = new Tile( row, col, (10 + (col * 40) + (col * 2)), (10 + (row * 40) + (row * 2)), 40, 40, i);
                    i = (i + 1) % 16;
                }
            }
        }

        public void Draw(System.Drawing.Graphics g)
        {
            foreach (var tile in board)
            {
                tile.draw( g );
            }
        }

        public Tile findZero()
        {
            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    if (board[row, col].value == 0)
                    {
                        return board[row, col];
                    }
                }
            }
            return board[0, 0];
        }

        public Tile getNeighbour( string direction, Tile zero )
        {
            int rowPos = 0;
            int colPos = 0;
            switch (direction)
            {
                case "left":
                    rowPos = zero.rowPos;
                    colPos = zero.colPos -1;
                    break;
                case "right":
                    rowPos = zero.rowPos;
                    colPos = zero.colPos +1;
                    break;
                case "up":
                    rowPos = zero.rowPos -1;
                    colPos = zero.colPos;
                    break;
                case "down":
                    rowPos = zero.rowPos +1;
                    colPos = zero.colPos;
                    break;
                default:
                    break;
            }

            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    if (board[row, col].rowPos == rowPos && board[row, col].colPos == colPos)
                    {
                        return board[row, col];
                    }
                }
            }

            return board[0, 0];
        }


        public void swap(Tile one, Tile other)
        {
            int temp;
            temp = one.value;
            one.value = other.value;
            other.value = temp;
        }

        public void randomMoves()
        {
            Tile zero = new Tile();
            Tile neighbour = new Tile();
            int randInt;
            Random rand = new Random();

            for (int i = 0; i < 1000000; i++)
            {
                
                randInt = rand.Next(1,5);

                zero = this.findZero();
                switch (randInt)
                {
                    case 1: // left
                        if (zero.colPos > 0)
                        {
                            neighbour = this.getNeighbour("left", zero);
                            this.swap(zero, neighbour);
                        }
                        break;

                    case 2: // right
                        if (zero.colPos < 3)
                        {
                            neighbour = this.getNeighbour("right", zero);
                            this.swap(zero, neighbour);
                        }
                        break;

                    case 3: // up
                        if (zero.rowPos > 0)
                        {
                            neighbour = this.getNeighbour("up", zero);
                            this.swap(zero, neighbour);
                        }
                        break;
                    case 4: // down
                        if (zero.rowPos < 3)
                        {
                            neighbour = this.getNeighbour("down", zero);
                            this.swap(zero, neighbour);
                        }
                        break;

                    default:
                        break;
                }

            }
        }

        public bool checkForWin()
        {
            int i = 1;
            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    if (board[row, col].value != i)
                    {
                        return false;
                    }
                    i = (i + 1) % 16;
                }
            }
            return true;
        }

    }
}
